import React from 'react';
import { connect } from 'dva';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import BatchUpload from '../../components/BatchUpload/Index';
import FileUpload from '../../components/BatchUpload/Upload';
import Finish from '../../components/BatchUpload/Finish';

@connect(({ oncechargecs, publicdata, loading }) => ({
  uploading: loading.effects['oncechargecs/uploadFileWithProgress'],
  batchQuery: oncechargecs.batchQuery,
  fileId: publicdata.downloadFile.fileId,
  fileName: publicdata.downloadFile.fileName,
  currentStep: oncechargecs.currentStep,
  progressPercent: oncechargecs.progressPercent,
  resultInfo: oncechargecs.resultInfo,
  showProgress: oncechargecs.showProgress,
}))
@Form.create()
class Batch extends React.PureComponent {
  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'oncechargecs/updateModelData',
      payload: { progressPercent: 0 },
    });
  }

  componentWillUnmount() {
    this.clearModel();
  }

  // 重置model
  clearModel = () => {
    const { dispatch } = this.props;
    dispatch({
      type: 'oncechargecs/updateModelData',
      payload: {
        currentStep: 0,
        progressPercent: 0,
        showProgress: false,
      },
    });
  };

  // 设置当前位置
  setCurrentstep = currentStep => {
    const { dispatch } = this.props;
    dispatch({
      type: 'oncechargecs/updateModelData',
      payload: { currentStep },
    });
    if (!currentStep) {
      this.clearModel();
    }
  };

  // 上传文件：需要携带参数即传：batchQuery
  uploadFile = formData => {
    const {
      dispatch,
      custOrSupp = 1,
      batchQuery: {
        onceFeeCustDraftId,
        pdId,
        feeWay,
        custId,
        custContId,
        busiYm,
        feeYm,
        onceFeeDrafId,
        ordSndrId,
      },
      checkFormSubmit,
    } = this.props;
    if (checkFormSubmit()) {
      let uploadQuery = {
        pdId,
        feeWay,
        custId,
        custContId,
        busiYm,
      };
      if (custOrSupp === 1) {
        uploadQuery = { ...uploadQuery, onceFeeCustDraftId: onceFeeCustDraftId || [] };
      } else {
        uploadQuery = { ...uploadQuery, onceFeeDrafId: onceFeeDrafId || [], ordSndrId };
      }
      if (+feeWay === 2) {
        uploadQuery = { ...uploadQuery, feeYm };
      }
      // 需要携带参数即传：batchQuery，不需要传参可省略
      Object.entries(uploadQuery).forEach(([key, value]) => {
        formData.append(key, value);
      });

      dispatch({
        type: 'oncechargecs/uploadFileWithProgress',
        payload: formData,
        custOrSupp,
      }).then(res => {
        if (res.success) {
          dispatch({
            type: 'oncechargecs/updateModelData',
            payload: { showProgress: true },
          });
        }
      });
    }
  };

  render() {
    const {
      uploading,
      currentStep,
      showProgress,
      resultInfo,
      progressPercent,
      closeUpload,
      fileId,
      fileName,
      finishClick,
    } = this.props;
    return (
      <BatchUpload currentStep={currentStep}>
        <FileUpload
          title="上传文件"
          currentStep={currentStep}
          setCurrentstep={this.setCurrentstep}
          downloadTempInfo={{
            // 下载模板信息
            params: { fileId, fileName },
            url: '/csApi/fileServer/downloadFile',
            type: 'GET',
          }}
          uploadFile={this.uploadFile}
          uploading={uploading}
          // 展示进度条需要的数据
          progressPercent={progressPercent || 0}
          progressTitle="上传文件"
          progressVisible={showProgress}
          preStep={() => {
            this.setCurrentstep(currentStep - 1);
            closeUpload();
          }}
        />
        <Finish
          title="完成"
          resultInfo={resultInfo}
          setCurrentstep={this.setCurrentstep}
          finishClick={() => {
            finishClick();
          }}
        />
      </BatchUpload>
    );
  }
}

export default Batch;
